package com.zchat.android;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Toast;

import com.zchat.android.app.BaseActivity;
import com.zchat.android.app.LauncherApplication;
import com.zchat.android.views.MenuBottom;
import com.zchat.android.views.ViewKeys;
import com.zchat.android.views.ViewOneLoader;
import com.zchat.api.log.Logger;
import com.zchat.api.util.Resolution;

/**
 * 
 * @author Oliverzhu
 *	2012/6/18
 */
public class WelcomeActivity extends BaseActivity {
	private static final String TAG = "WelcomeActivity";
	private FrameLayout container;
	
	private LayoutInflater inflater;
	
	private FrameLayout mainFrame;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.welcome_container);
		
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected void initComponents() {
		//整个布局容器
		container = (FrameLayout) findViewById(R.id.welcomeFrame);
		//set the default main layout
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View defaultView = inflater.inflate(R.layout.welcome, container);
		//底部工具栏
		final MenuBottom toolbarBottom = (MenuBottom) defaultView.findViewById(R.id.toolbarBottom);
		
		toolbarBottom.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, (int)(Resolution.getInstance().getScreenHeight() * 0.12)));
		
		mainFrame = (FrameLayout) defaultView.findViewById(R.id.contentFrame);
		
		zchat.getCache().put("container", container);
		zchat.getCache().put("paperFrame", mainFrame);
		
	}

	@Override
	protected void initDatas() {
		ViewOneLoader.getInstance().startLoader(context, mainFrame, ViewKeys.MENU_VIEW_ONE, zchat);
	}

	@Override
	protected void setComponentListeners() {
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		
		zchat.getCache().put("container", container);
		zchat.getCache().put("paperFrame", mainFrame);
	}
	
	private long firstTime;
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if(System.currentTimeMillis() - firstTime > 3000) {
				Toast.makeText(context, "再按一次返回键退出应用程序", 3000).show();
				firstTime = System.currentTimeMillis();
				return true;
			} else {
				if(DEBUG) Logger.info(TAG+"-exit zchat");
				finish();
				System.exit(0);
				return true;
			}
		
		default:
			break;
		}
		return true;
	}
	
	@Override
	public String toString() {
		return TAG;
	}

}
